﻿using System;
using com.nm.Helper;
using george.Web.Helper;

// ReSharper disable once CheckNamespace
namespace com.nm.ApiServer.Attributes {
	/// <summary>
	/// 接受JSON数据
	/// </summary>
	[AttributeUsage(AttributeTargets.Method)]
	// ReSharper disable once InconsistentNaming
	public class JSONDataAttribute : Attribute {
	}
	/// <summary>
	/// 输入的数据需要从JSON字串转换到对象
	/// </summary>
	[AttributeUsage(AttributeTargets.Method)]
	public class JsonTypeAttribute : Attribute {
		/// <summary>
		/// 解析JSON对象
		/// </summary>
		/// <param name="jsonString"></param>
		/// <param name="toType"></param>
		/// <param name="type"></param>
		/// <returns></returns>
		public static object Parse(string jsonString, Type toType, int type) {
			if (type == 1) {
				var obj = QueryStringHelper.ParseQueryToJson(jsonString);
				jsonString = obj.ToJson();
			}
			return jsonString.FromJson(toType);
		}
	}
	/// <summary>
	/// 标识API返回类型
	/// </summary>
	[AttributeUsage(AttributeTargets.Method)]
	public class ReturnTypeAttribute : Attribute {
		/// <summary>
		/// 返回的类型
		/// </summary>
		public Type ReturnType { get; set; }
		/// <summary>
		/// 描述
		/// </summary>
		public string Description { get; set; }
		/// <summary>
		/// 初始化
		/// </summary>
		/// <param name="type"></param>
		/// <param name="description"></param>
		public ReturnTypeAttribute(Type type, string description = "") {
			this.ReturnType = type;
			this.Description = description;
		}
	}
}
